package gam.t1.appraisal2.reninv;


import gam.t1.appraisal2.Data;
import gam.t1.appraisal2.dinv.AppraisalOption;
import gam.t1.appraisal2.dinv.DataInventory;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;

import java.util.Map;

public final class RenderInventory {
    private static Inventory CheckOpenInventory(Player player){
        Inventory v1 = Data.getInventory(player.getUniqueId());
        if (v1 == null) {
            v1 = Bukkit.createInventory(player, 54);
            Data.addInventory(player.getUniqueId(), v1);
        }
        return v1;
    }
    public static void render(Map<Integer,ItemStack> itemMap, Player player) {
        if (itemMap != null){
            Inventory var1 = CheckOpenInventory(player);
            DataInventory var2 = Data.getAppraisalDataInventory(player.getUniqueId());
            if (var2.getView() == null) var2.setView(var1);
            for (Integer index : itemMap.keySet()) {
                ItemStack var3 = itemMap.get(index);
                var1.setItem(index, var3);
            }
            ItemStack[] cs = var1.getContents();
            for (int i = 0; i < cs.length; i++) if (cs[i] == null && !var2.hasOccupied(i)) var1.setItem(i, AppraisalOption.EMPTY);
            if (player.getOpenInventory().getTopInventory().equals(var1)){
                player.getOpenInventory().setTitle(var2.getInventoryName());
                player.updateInventory();
            } else {
                player.openInventory(var1);
                player.getOpenInventory().setTitle(var2.getInventoryName());
            }
        }
    }
}
